package com.chenchao.javageneric;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Random;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/13 13:24
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javageneric.SetTest.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class SetTest {
    public static void main(String[] args) {
        //test1();
        //equalsInSet();
        randomSet();
    }
    public static void test1(){
        //set元素无序不重复
        HashSet<String> strings = new HashSet<>();
        boolean b1 = strings.add("a");
        boolean b2 = strings.add("a");
        System.out.println(b1);
        System.out.println(b2);
        //第一次添加成功，第二次添加失败
        boolean b3 = strings.add("b");
        boolean b4 = strings.add("c");
        boolean b5 = strings.add("d");
        boolean b6 = strings.add("1");
        System.out.println(strings);
        //[a, 1, b, c, d]无序

        //迭代器遍历
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //for循环
        for (String string : strings) {
            System.out.println(string);
        }
    }
    /**
     * 判断set中两个元素相同（去重）
     * 判断规则hashcode和equals同时满足
     * 不重写的话，Object给相同类型对象生成的hashcode不同
     * 重写的话，相同属性的每个对象的hashcode相同
     * hashcode就是跟内存相关的编号
     * 每一个对象的hashcode(系统生成)都不一样
     */
    public static void equalsInSet(){
        HashSet<Stu> stus = new HashSet<>();
        Stu stu1 = new Stu("张三", 14);
        Stu stu2 = new Stu("张三", 14);
        Stu stu3 = new Stu("张三1", 14);
        Stu stu4 = new Stu("张三1", 14);
        Stu stu5 = new Stu("张三1", 14);
        stus.add(stu1);
        //添加时先判断hashcode是否相同，相同再比较equals()
        //不同就直接添加
        stus.add(stu2);
        stus.add(stu3);
        stus.add(stu4);
        stus.add(stu5);
        System.out.println(stu1.hashCode());
        System.out.println(stu2.hashCode());
        System.out.println(stu3.hashCode());
        System.out.println(stu4.hashCode());
        System.out.println(stu5.hashCode());
        System.out.println(stus);
    }
    /**
     * 生成0~20的10个不重复的随机数
     */
    public static void randomSet(){
        Random random = new Random();
        HashSet<Integer> integers = new HashSet<>();
        //次数不确定用while
        while (integers.size()<10){
            //自动装箱
            integers.add(random.nextInt(20)+1);
            //添加时会执行去重操作
        }
        System.out.println(integers);
    }


}
class Stu{
    String name;
    int age;

    public Stu() {
    }

    public Stu(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("equals方法执行了");
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Stu stu = (Stu) o;
        return age == stu.age &&
                Objects.equals(name, stu.name);
    }
    @Override
    public int hashCode() {
        System.out.println("hascode方法执行了");
        return Objects.hash(name, age);
    }

}
